package com.dragon.logical.service.auth.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dragon.repository.entity.Calendared;
import com.dragon.repository.mapper.CalendarMapper;
import com.dragon.logical.service.auth.ICalendarService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author sy
 * @since 2023-07-19
 */
@Service
public class CalendarServiceImpl extends ServiceImpl<CalendarMapper, Calendared> implements ICalendarService {
    
    @Autowired
    CalendarMapper mapper;

    private final static String DATE_API_URL = "https://opendata.baidu.com/data/inner";


    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public void calendarUpdate(Integer years) {
        QueryWrapper<Calendared> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(Calendared::getYear,years);
        List<Calendared> calendaredList = mapper.selectList(queryWrapper);
        if(calendaredList.size()!=0&& calendaredList !=null){
            mapper.delete(queryWrapper);
        }
        List<Integer> months = Arrays.asList(2, 5, 8, 11);
        Map<String, Object> params = new HashMap<>();
        params.put("tn", "reserved_all_res_tn");
        params.put("type", "json");
        params.put("resource_id", "52109");
        params.put("apiType", "yearMonthData");
        months.forEach(months1 ->{
            String query = String.valueOf(years).concat("年");
            query = query.concat(String.valueOf(months1)).concat("月");
            params.put("query",query);
            String result = HttpUtil.get(DATE_API_URL, params);
            JSONObject jsonObject = JSONObject.parseObject(result, JSONObject.class);
            JSONArray jsonArray = (JSONArray) jsonObject.get("Result");
            JSONObject object = jsonArray.getJSONObject(0);
            JSONObject object1 = object.getJSONObject("DisplayData")
                    .getJSONObject("resultData")
                    .getJSONObject("tplData")
                    .getJSONObject("data");
            JSONArray resultData = (JSONArray) object1.get("almanac");
            List<Calendared> calendaredLists = JSONObject.parseArray(JSONObject.toJSONString(resultData), Calendared.class);
            this.saveBatch(calendaredLists);
        });
    }


    @Override
    public Calendared isWorkDay(String dateString){
        Date date = DateUtil.parse(dateString);
        QueryWrapper<Calendared> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(Calendared::getYear, DateUtil.year(date))
                .eq(Calendared::getMonth,DateUtil.month(date)+1)
                .eq(Calendared::getDay,DateUtil.dayOfMonth(date));
        Calendared calendared =  mapper.selectOne(queryWrapper);

        Boolean isWorkDay = false;
        if(calendared.getStatus()==null){
            Integer weeks = DateUtil.dayOfWeek(date);
            if(weeks == 1 || weeks == 7){
                isWorkDay = false;
            } else {
                isWorkDay = true;
            }
        }
        if(calendared.getStatus() != null && calendared.getStatus() == 2){
            isWorkDay = true;
        }
        calendared.setIsWorkDay(isWorkDay);
        return calendared;
    }

}
